﻿using QD.Snapshot.Core.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace QD.Snapshot.File
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                //设置应用程序处理异常方式：ThreadException处理
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                //处理非UI线程异常
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                #region
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
                #endregion
            }
            catch (Exception e)
            {
                WriteText("总程序异常问题：" + e.StackTrace + "\r\n");
            }
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            WriteText("应用程序异常问题：" + e.Exception + "\r\n");
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            WriteText("非UI线程异常问题：" + ((Exception)e.ExceptionObject).Message + "\r\n" + ((Exception)e.ExceptionObject).StackTrace);
        }

        static void WriteText(string text)
        {
            Pigeon.Utility.Log.Write(string.Format("{0}_{1}.log", "error", DateTime.Now.ToString("yyyyMMdd")), text);
        }
    }
}
